// cFinalPage.cpp : implementation file
//

#include "stdafx.h"
#include "QPMig.h"
#include "cFinalPage.h"
#include "cNotesFolderInfo.h"
#include "cQuickPlaceDB.h"


#include "noteshelperfunctions.h"
#include "helper.h"

extern CQPMigApp theApp;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// cFinalPage property page

IMPLEMENT_DYNCREATE(cFinalPage, CPropertyPageEx)

cFinalPage::cFinalPage() : CPropertyPageEx(cFinalPage::IDD,0,IDS_QPLOGHEADERTITLE,IDS_QPLOGHEADERSUBTITLE)
{
	//{{AFX_DATA_INIT(cFinalPage)
	m_strResult = _T("");
	//}}AFX_DATA_INIT
}

cFinalPage::~cFinalPage()
{
}

void cFinalPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(cFinalPage)
	DDX_Text(pDX, IDC_EDIT1, m_strResult);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(cFinalPage, CPropertyPageEx)
	//{{AFX_MSG_MAP(cFinalPage)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// cFinalPage message handlers

BOOL cFinalPage::OnSetActive() 
{
	CString cStatus;
	COleDateTime codt=COleDateTime::GetCurrentTime();
	cStatus.Format("The export starts at %s",codt.Format()); 
	theApp.stControl.pLog->LogOneLine(cStatus);
	theApp.stControl.pDebugLog->LogOneLine(cStatus);
	theApp.stControl.pDebugLog->Flush();

	CPropertySheet* psheet = (CPropertySheet*) GetParent();   
  psheet->SetWizardButtons(PSWIZB_FINISH);
	CFileStatus status;
	CWaitCursor cw;
	MSXML2::IXMLDOMDocument2Ptr pXMLDoc=NULL;
	MSXML2::IXMLDOMElementPtr pNode=NULL;
	HRESULT hr=pXMLDoc.CreateInstance(__uuidof(DOMDocument40));
	pXMLDoc->setProperty("SelectionLanguage", "XPath");

MSXML2::IXMLDOMProcessingInstructionPtr pPI = pXMLDoc->createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

	_variant_t vNullVal;
	vNullVal.vt = VT_NULL;
	pXMLDoc->insertBefore(pPI, vNullVal);
	pPI.Release();
	MSXML2::IXMLDOMElementPtr pRootContainerElem=NULL;
	MSXML2::IXMLDOMElementPtr pRootElem=pXMLDoc->createElement(L"Import");
	MSXML2::IXMLDOMElementPtr pSettingsContainerElem=NULL;
	CString cfname;

	//Fix displaynames
	
	if(pRootElem)
		{
			pXMLDoc->documentElement=pRootElem;
			pSettingsContainerElem=pXMLDoc->createElement(L"settings");
			pNode=pXMLDoc->createElement(L"locale");
			LCID lcid=GetThreadLocale() ;
			CString cLCID;
			cLCID.Format("%d",lcid);

			BSTR bstr=cLCID.AllocSysString();

			pNode->put_text(bstr);
			::SysFreeString(bstr);
			pSettingsContainerElem->appendChild(pNode);
			pNode.Release();
			pRootElem->appendChild(pSettingsContainerElem);
			pSettingsContainerElem.Release();

			pRootContainerElem=pXMLDoc->createElement(L"rootcontainer");
			pNode=pXMLDoc->createElement(L"type");
			if(theApp.stControl.m_strContainerType=="Area")
				pNode->put_text(L"Area");
			else
				pNode->put_text(L"Site");
			pRootContainerElem->appendChild(pNode);
			pNode.Release();

			pNode=pXMLDoc->createElement(L"name");
			pNode->put_text(theApp.stControl.bstrSiteContainerRoot);
			pRootContainerElem->appendChild(pNode);
			pNode.Release();
			pRootElem->appendChild(pRootContainerElem);
					

			cfname.Format("%s%s",theApp.stControl.m_strDirectoryPath,"result.txt");
			theApp.stControl.pDebugLog->LogOneLine("Enumerating through the databases");
			for(int i=0;i<=theApp.stControl.m_objQuickPlaces.GetUpperBound();i++)
				{
					cQuickPlaceDB *pdb=(cQuickPlaceDB *)theApp.stControl.m_objQuickPlaces.GetAt(i);
					if(pdb)
						{
							pdb->cArUnids.RemoveAll();
							theApp.stControl.m_strCurDir=theApp.stControl.m_strDirectoryPath+theApp.stControl.m_strQPName;
							theApp.stControl.pdb=pdb;
							theApp.stControl.m_strQPName=pdb->strName;
							theApp.stControl.pDebugLog->LogOneLine(pdb->strName);
							theApp.stControl.pDebugLog->Flush();
							try
								{
									ProcessDB(pdb,pXMLDoc,pRootContainerElem,pRootElem,cfname);
								}
							catch(CException* e)
								{
									theApp.stControl.pDebugLog->LogOneLine("Exception when processing the database");
									theApp.stControl.pDebugLog->LogOneLine(pdb->strName);
									theApp.stControl.pDebugLog->Flush();
									char szError[255];
									
									if(e->GetErrorMessage(szError,255))
										theApp.stControl.pDebugLog->LogOneLine(szError);
									theApp.stControl.pDebugLog->Flush();
									e->ReportError();
									e->Delete();


								}

						}
				}


			
		}
	codt=COleDateTime::GetCurrentTime();
	cStatus.Format("The export ended at %s after exporting %d documents and %d list items",codt.Format(),theApp.stControl.NrOfDocs,theApp.stControl.NrOfListItems); 
	theApp.stControl.pLog->LogOneLine(cStatus);

	theApp.stControl.pLog->Close();

	CString cf;
	cf.Format("%s\\%s",theApp.stControl.m_strDirectoryPath,"struct.xml");
	BSTR bstr=cf.AllocSysString();
	pXMLDoc->save(bstr);
	::SysFreeString(bstr);
	pRootContainerElem.Release();
	pXMLDoc.Release();


	//FILE *fpResult=fopen("c:\\result.txt","rb");
	if( CFile::GetStatus( cfname, status ) )   // static function
		 {
				#ifdef _DEBUG
					 afxDump << "Full file name = " << status.m_szFullName << " size ="<< status.m_size << "\n";
				#endif
		}
	char *pstr=(char *)malloc(status.m_size+1);   
	CFile cfile(cfname,CFile::modeRead);
	UINT uintread=cfile.Read(pstr,status.m_size);
	pstr[uintread]='\0';
	m_strResult =pstr;
	free(pstr);	
	cfile.Close();

	UpdateData(FALSE);

	return CPropertyPageEx::OnSetActive();
}



BOOL cFinalPage::OnWizardFinish() 
{
	
	
	return CPropertyPageEx::OnWizardFinish();
}

